package com.boot2.core.conf;

import com.boot2.core.constant.CoreConstant;
import com.boot2.core.constant.CoreUrlConstant;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @author zhangweilin
 * @ClassName: UrlConf
 * @description: 与url有关的配置
 * @date 2017年12月14日 下午2:00:42
 */
@Data
//@Configuration
//@ConfigurationProperties(prefix = "sys.core.url")
//@ConditionalOnMissingBean(name="hlChannelInitializer")   //tcp下，忽略url配置
public class UrlProperties {

    @Autowired
    UploadUrlConf uploadUrlConf;
    /**
     * 代理ip
     */
    private String proxyIp;

    /**
     * 代理端口
     */
    private Integer proxyPort;

    @Autowired
    private Environment environment;

    /**
     * 顶级域名
     */
    private String domain;

    /**
     * 主页地址
     */
    private String indexPage;

    /**
     * 资源url
     */
    private String resUrl;

    /**
     * api url
     */
    private String apiUrl;

    /**
     * 业务系统自定义路径
     */
    private Map<String, String> custom;

    /**
     * 是否启用了https,
     */
//	private Boolean isHttps = false;
    private String protocol = "http://";

    /**
     * 登录页.默认跳 /login
     */
    private String loginPageUrl = "/login";

    /**
     * aes加密的key,用来给id和敏感url加密
     */
    public static String aesEncryptKey = "hl_ark_iot_2017-12-22 17:36:52";

    /**
     * 当前激活的active,会自动追加在所有url前面，如果为生产环境，则不追加
     */
    @PostConstruct
    public void init() {
        //真正的环境，只认第一个
        String[] activeProfiles = environment.getActiveProfiles();
        Assert.notNull(activeProfiles, "请配置 spring.profiles.active ");
        String profile = activeProfiles[0];
        profile = CoreConstant.prod.equalsIgnoreCase(profile) ? "" : profile + ".";
        CoreUrlConstant.domain = domain;
        CoreUrlConstant.loginPageUrl = loginPageUrl;
        CoreUrlConstant.protocol = protocol;
//		CoreUrlConstant.indexPage = CoreUrlConstant.protocol + profile+"www." + domain;
        CoreUrlConstant.indexPage = indexPage;
        CoreUrlConstant.resUrl = resUrl;
        CoreUrlConstant.apiUrl = apiUrl;
        CoreUrlConstant.picUrl = uploadUrlConf.getEndPoint();
//		CoreUrlConstant.resUrl = CoreUrlConstant.protocol + profile+"res." + domain;
//		CoreUrlConstant.apiUrl= CoreUrlConstant.protocol + profile+"api." + domain;


    }
}
